package com.cloud.consul.comsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceController {
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	@Autowired
	private DiscoveryClient disconveryClient;

	/**
	 * 获取所有的服务
	 * 
	 * @return
	 */
	@RequestMapping("/services")
	public Object services() {
		return disconveryClient.getInstances("spring-cloud-consul-producer");
	}

	/**
	 * 从所有服务中选择一个服务（轮询）
	 * 
	 * @return
	 */
	@RequestMapping("discover")
	public Object discover() {
		return loadBalancerClient.choose("spring-cloud-consul-producer").getUri().toString();
	}
}
